package at.fhj.mail;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.PasswordAuthentication;

public class MailService {
	
	private static MailService theService = null;

	private static Session mailSession;

	private static final String HOST = "smtp.gmail.com";
	private static final int PORT = 587;
	private static final String USER = "kevin.ballach@gmail.com"; // Must be valid user in d.umn.edu
											// domain, e.g. "smit0012"
	private static final String PASSWORD = "nachtfalke"; // Must be valid password for
												// smit0012
	private static final String FROM = "Kevin Ballach <kevin.ballach@gmail.com"; // Full info for user, e.g.
											// "Fred Smith <smit0012@d.umn.edu>"

	
	

	public static void sendMessage(String recipient, String subject,
			String message) throws MessagingException {

		if (theService == null) {
			theService = new MailService();
		}

		MimeMessage mimeMessage = new MimeMessage(mailSession);

		mimeMessage.setFrom(new InternetAddress(FROM));
		mimeMessage.setSender(new InternetAddress(FROM));
		mimeMessage.setSubject(subject);
		mimeMessage.setContent(message, "text/plain");
		mimeMessage.setText("TEST");

		mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(
				recipient));

		Transport transport = mailSession.getTransport("smtps");
		transport.connect(HOST, PORT, USER, PASSWORD);

		transport.sendMessage(mimeMessage,
				mimeMessage.getRecipients(Message.RecipientType.TO));
		System.out.println("Done");
		transport.close();

	}

	private MailService() {
		Properties props = new Properties();

		props.put("mail.transport.protocol", "smtps");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtps.host", HOST);
		props.put("mail.smtps.auth", "true");
		props.put("mail.smtp.from", FROM);
		//props.put("mail.smtps.quitwait", "false");
		
		
		mailSession = Session.getDefaultInstance(props, new javax.mail.Authenticator(){
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(USER, PASSWORD);
				
			}
		});
		mailSession.setDebug(true);
	}

}
